package com.songcha.module_category.ui.activity.category_detail

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.songcha.library_base.mvvm.base.BaseRefreshLoadMoreMvvmActivity
import com.songcha.library_business.interfaces.IOnBookClickListener
import com.songcha.library_business.ui.adapter.BookListAdapter
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_common.util.ColorUtil
import com.songcha.library_common.util.dp2px
import com.songcha.module_category.R
import com.songcha.module_category.databinding.CategoryActivityDetailBinding

class CategoryDetailActivity:BaseRefreshLoadMoreMvvmActivity<CategoryDetailViewModel,CategoryActivityDetailBinding>(),OnClickListener,IOnBookClickListener {
    private var mAdapter:BookListAdapter?=null
    private var mSelectBorderColor:Int=Color.TRANSPARENT
    private var mSelectTextColor:Int=Color.TRANSPARENT
    private var mNormalTextColor:Int=Color.parseColor("#635F5F")

    override fun getLayoutId(): Int {
        return R.layout.category_activity_detail
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        intent?.let {
            viewModel.title=it.getStringExtra("title")?:""
            viewModel.channelSubId=it.getIntExtra("channel_sub_id",0)
        }

        if(viewModel.channelSubId<=0) return

        mSelectTextColor = ColorUtil.getResourceColor(this, com.songcha.library_common.R.color.primary)

        if(viewModel.title.isNotBlank()){
            databinding.categoryDetailHbv.setTitle(viewModel.title)
            databinding.categoryTvAll2.text=viewModel.title
        }

        databinding.categoryCclAllAll.setOnClickListener(this)
        databinding.categoryCclAll2.setOnClickListener(this)
        databinding.categoryCclUpdateStatusAll.setOnClickListener(this)
        databinding.categoryCclUpdateStatus2.setOnClickListener(this)
        databinding.categoryCclUpdateStatus3.setOnClickListener(this)
        databinding.categoryCclTextCountAll.setOnClickListener(this)
        databinding.categoryCclTextCount2.setOnClickListener(this)
        databinding.categoryCclTextCount3.setOnClickListener(this)
        databinding.categoryCclTextCount4.setOnClickListener(this)
        databinding.categoryCclTextCount5.setOnClickListener(this)

        mSelectBorderColor=ColorUtil.getResourceColor(this, com.songcha.library_common.R.color.primary)
    }

    override fun getLoadBackgroundColor(): Int {
        return Color.WHITE
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    override fun onGetData() {
        if(viewModel.channelSubId<=0) return
        viewModel.getCategoryDetailBookList()
    }

    override fun onGetLoadMoreData() {
        if(viewModel.channelSubId<=0) return
        viewModel.getCategoryDetailBookList()
    }

    override fun getLoadViewTopMargin(): Int {
        if(isViewModelInitialized()){
            val point=IntArray(2)
            databinding.categoryDetailRcv.getLocationInWindow(point)
            return point[1]
        } else
            return 0
    }

    override fun initListData() {
        if(viewModel.rlst.value==null) return
        databinding.categoryDetailRcv.apply{
            layoutManager=LinearLayoutManager(context)
            mAdapter=BookListAdapter(viewModel.rlst.value!!,this@CategoryDetailActivity,false)
            adapter=mAdapter
        }
    }

    override fun getRefreshLayout(): SmartRefreshLayout {
        return databinding.categoryDetailSrl
    }

    override fun getListAdapter(): RecyclerView.Adapter<BaseViewHolder>? {
        return mAdapter
    }

    override fun isImmerseStatusBar(): Boolean {
        return true
    }



    fun setUpdateStatusAllStyle(){
        if(viewModel.isLoading || viewModel.isRefreshing || viewModel.isLoadingMore) return
        databinding.categoryCclUpdateStatusAll.setBorder(mSelectTextColor,0.5f.dp2px())
        databinding.categoryCclUpdateStatus2.setBorder(Color.TRANSPARENT,0f)
        databinding.categoryCclUpdateStatus3.setBorder(Color.TRANSPARENT,0f)

        databinding.categoryTvUpdateStatusAll.setTextColor(mSelectTextColor)
        databinding.categoryTvUpdateStatus2.setTextColor(mNormalTextColor)
        databinding.categoryTvUpdateStatus3.setTextColor(mNormalTextColor)
    }

    fun setTextCountAllStyle(){
        if(viewModel.isLoading || viewModel.isRefreshing || viewModel.isLoadingMore) return
        databinding.categoryCclTextCountAll.setBorder(mSelectTextColor,0.5f.dp2px())
        databinding.categoryCclTextCount2.setBorder(Color.TRANSPARENT,0f)
        databinding.categoryCclTextCount3.setBorder(Color.TRANSPARENT,0f)
        databinding.categoryCclTextCount4.setBorder(Color.TRANSPARENT,0f)
        databinding.categoryCclTextCount5.setBorder(Color.TRANSPARENT,0f)

        databinding.categoryTvTextCountAll.setTextColor(mSelectTextColor)
        databinding.categoryTvTextCount2.setTextColor(mNormalTextColor)
        databinding.categoryTvTextCount3.setTextColor(mNormalTextColor)
        databinding.categoryTvTextCount4.setTextColor(mNormalTextColor)
        databinding.categoryTvTextCount5.setTextColor(mNormalTextColor)
    }

    override fun onClick(v: View) {
        when(v.id){
            R.id.category_ccl_all_all->{
                if(viewModel.isLoading || viewModel.isRefreshing || viewModel.isLoadingMore) return
                databinding.categoryCclAllAll.setBorder(mSelectBorderColor,0.5f.dp2px())
                databinding.categoryCclAll2.setBorder(Color.TRANSPARENT,0f)

                databinding.categoryTvAllAll.setTextColor(mSelectTextColor)
                databinding.categoryTvAll2.setTextColor(mNormalTextColor)

                viewModel.currentPage=1
                viewModel.status=0
                viewModel.wordsStart=0
                viewModel.wordsEnd=0

                setUpdateStatusAllStyle()
                setTextCountAllStyle()

                viewModel.getCategoryDetailBookList()

            }
            R.id.category_ccl_all2->{
                if(viewModel.isLoading || viewModel.isRefreshing || viewModel.isLoadingMore) return
                databinding.categoryCclAll2.setBorder(mSelectBorderColor,0.5f.dp2px())
                databinding.categoryCclAllAll.setBorder(Color.TRANSPARENT,0f)

                databinding.categoryTvAll2.setTextColor(mSelectTextColor)
                databinding.categoryTvAllAll.setTextColor(mNormalTextColor)

                viewModel.currentPage=1
                viewModel.status=0
                viewModel.wordsStart=0
                viewModel.wordsEnd=0

                setUpdateStatusAllStyle()
                setTextCountAllStyle()

                viewModel.getCategoryDetailBookList()
            }
            R.id.category_ccl_update_status_all->{
                setUpdateStatusAllStyle()

                viewModel.currentPage=1
                viewModel.status=0
                viewModel.getCategoryDetailBookList()
            }
            R.id.category_ccl_update_status2->{
                if(viewModel.isLoading || viewModel.isRefreshing || viewModel.isLoadingMore) return
                databinding.categoryCclUpdateStatus2.setBorder(mSelectBorderColor,0.5f.dp2px())
                databinding.categoryCclUpdateStatusAll.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclUpdateStatus3.setBorder(Color.TRANSPARENT,0f)

                databinding.categoryTvUpdateStatus2.setTextColor(mSelectTextColor)
                databinding.categoryTvUpdateStatusAll.setTextColor(mNormalTextColor)
                databinding.categoryTvUpdateStatus3.setTextColor(mNormalTextColor)

                viewModel.currentPage=1
                viewModel.status=2
                viewModel.getCategoryDetailBookList()
            }
            R.id.category_ccl_update_status3->{
                if(viewModel.isLoading || viewModel.isRefreshing || viewModel.isLoadingMore) return
                databinding.categoryCclUpdateStatus3.setBorder(mSelectBorderColor,0.5f.dp2px())
                databinding.categoryCclUpdateStatus2.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclUpdateStatusAll.setBorder(Color.TRANSPARENT,0f)

                databinding.categoryTvUpdateStatus3.setTextColor(mSelectTextColor)
                databinding.categoryTvUpdateStatus2.setTextColor(mNormalTextColor)
                databinding.categoryTvUpdateStatusAll.setTextColor(mNormalTextColor)

                viewModel.currentPage=1
                viewModel.status=1
                viewModel.getCategoryDetailBookList()
            }
            R.id.category_ccl_textCount_all->{
                setTextCountAllStyle()
                viewModel.currentPage=1
                viewModel.wordsStart=0
                viewModel.wordsEnd=0
                viewModel.getCategoryDetailBookList()
            }
            R.id.category_ccl_textCount2->{
                if(viewModel.isLoading || viewModel.isRefreshing || viewModel.isLoadingMore) return
                databinding.categoryCclTextCount2.setBorder(mSelectBorderColor,0.5f.dp2px())
                databinding.categoryCclTextCountAll.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCount3.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCount4.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCount5.setBorder(Color.TRANSPARENT,0f)

                databinding.categoryTvTextCount2.setTextColor(mSelectTextColor)
                databinding.categoryTvTextCountAll.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCount3.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCount4.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCount5.setTextColor(mNormalTextColor)

                viewModel.currentPage=1
                viewModel.wordsStart=0
                viewModel.wordsEnd=500000
                viewModel.getCategoryDetailBookList()
            }
            R.id.category_ccl_textCount3->{
                if(viewModel.isLoading || viewModel.isRefreshing || viewModel.isLoadingMore) return
                databinding.categoryCclTextCount3.setBorder(mSelectBorderColor,0.5f.dp2px())
                databinding.categoryCclTextCount2.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCountAll.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCount4.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCount5.setBorder(Color.TRANSPARENT,0f)

                databinding.categoryTvTextCount3.setTextColor(mSelectTextColor)
                databinding.categoryTvTextCount2.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCountAll.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCount4.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCount5.setTextColor(mNormalTextColor)

                viewModel.currentPage=1
                viewModel.wordsStart=500000
                viewModel.wordsEnd=1000000
                viewModel.getCategoryDetailBookList()
            }
            R.id.category_ccl_textCount4->{
                if(viewModel.isLoading || viewModel.isRefreshing || viewModel.isLoadingMore) return
                databinding.categoryCclTextCount4.setBorder(mSelectBorderColor,0.5f.dp2px())
                databinding.categoryCclTextCount2.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCount3.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCountAll.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCount5.setBorder(Color.TRANSPARENT,0f)

                databinding.categoryTvTextCount4.setTextColor(mSelectTextColor)
                databinding.categoryTvTextCount2.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCount3.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCountAll.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCount5.setTextColor(mNormalTextColor)

                viewModel.currentPage=1
                viewModel.wordsStart=1000000
                viewModel.wordsEnd=3000000
                viewModel.getCategoryDetailBookList()
            }
            R.id.category_ccl_textCount5->{
                if(viewModel.isLoading || viewModel.isRefreshing || viewModel.isLoadingMore) return
                databinding.categoryCclTextCount5.setBorder(mSelectBorderColor,0.5f.dp2px())
                databinding.categoryCclTextCount2.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCount3.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCount4.setBorder(Color.TRANSPARENT,0f)
                databinding.categoryCclTextCountAll.setBorder(Color.TRANSPARENT,0f)

                databinding.categoryTvTextCount5.setTextColor(mSelectTextColor)
                databinding.categoryTvTextCount2.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCount3.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCount4.setTextColor(mNormalTextColor)
                databinding.categoryTvTextCountAll.setTextColor(mNormalTextColor)

                viewModel.currentPage=1
                viewModel.wordsStart=3000000
                viewModel.wordsEnd=0
                viewModel.getCategoryDetailBookList()
            }
        }
    }

    override fun onBookClick(bookId: String, img: ImageView) {
        BusinessJumpUtil.toBookDetail(bookId)
    }

}